//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package upgrade

import (
	"context"

	"gitcode.com/eteam/api-server/dao"
	"gitcode.com/eteam/api-server/dao/board_dao"
	"gitcode.com/eteam/api-server/dao/fs_dao"
	"gitcode.com/eteam/proto-gen-go.git/fs_api"
	"github.com/google/uuid"
)

type UpToVer2 struct{}

func (up *UpToVer2) Exec(ctx context.Context) error {
	boardItemList, err := board_dao.BoardInfoDao.ListAll(ctx)
	if err != nil {
		return err
	}
	for _, boardItem := range boardItemList {
		if boardItem.ImageFsId != "" {
			continue
		}
		boardItem.ImageFsId = uuid.NewString()
		err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
			FsId:      boardItem.ImageFsId,
			OwnerType: fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_BOARD,
			OwnerId:   boardItem.BoardId,
			Status: fs_dao.FsStatusDbItem{
				FileCount:     0,
				TotalFileSize: 0,
			},
			MaxFilecount: 0,
			MaxTotalSize: 0,
			LastGcTime:   0,
		})
		if err != nil {
			return err
		}
		err = board_dao.BoardInfoDao.UpdateImageFsId(ctx, boardItem.BoardId, boardItem.ImageFsId)
		if err != nil {
			return err
		}
	}
	return dao.VersionDao.Set(ctx, dao.VERSION_KEY_SCHEMA, 2)
}
